#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File  :messages_push.py
# Time  :2024/11/26 16:28
# Email :fortunatedong@163.com
# Author:by Adonli
from datetime import datetime

import requests
import toml
from jingdong_order_synchronization_kuaipu.settings.logger import Loggings
logs = Loggings()

class MessagePush():
    def __init__(self,message):
        self._cfg = toml.load('settings/config.toml')
        self._robot_url = self._cfg['DINGROBOT']['robot_url']
        self._company_robot_url = self._cfg['DINGROBOT']['company_robot_url']
        self.message = message

    def success_text_push(self):
        message_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        title = "京东商城订单通知"
        branch_id = self.message["request"]["add_jingdong_order"]['branch_id']
        jingdong_order_id = self.message["request"]["add_jingdong_order"]['jingdong_order_id']
        order_create_date = self.message["request"]["add_jingdong_order"]['order_create_date']
        jingdong_ware_count = len(self.message["request"]["add_jingdong_order_bodys"]["add_jingdong_order_body"])
        self._robot_body = {
            "msgtype": "markdown",
            "markdown": {
                "title": title,
                "text": f"### {title}\n\n"
                        f"- #### 京东订单号：<font color=\"#4169E1\">{jingdong_order_id}</font>\n\n"
                        f"- #### 下单时间：<font color=\"#4169E1\">{order_create_date}</font>\n\n"
                        f"- #### 商品数量：<font color=\"#4169E1\">{jingdong_ware_count}</font>\n\n"
                        f"- #### 是否同步快普：<font color=\"#00EE76\">是</font>\n\n"
                        f"- #### 机构：<font color=\"#9400D3\">{branch_id}</font>\n\n"
                        f"- 机器人通知时间：<font color=\"#4F4F4F\">{message_time}</font>\n\n"
                        f"@xxxxxxxxxxxxxx",
            },
            "at": {
                "isAtAll": "false",
                "atMobiles": ["xxxxxxxxxxxxxxxx"]
            },
        }
        # logs.info(self._robot_body)
        res = requests.post(url=self._robot_url, json=self._robot_body)
        logs.info(f"钉钉通知状态：{res.json()}")

    def failed_text_push(self):
        message_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        title = "京东商城订单通知"
        branch_id = self.message["request"]["add_jingdong_order"]['branch_id']
        jingdong_order_id = self.message["request"]["add_jingdong_order"]['jingdong_order_id']
        order_create_date = self.message["request"]["add_jingdong_order"]['order_create_date']
        jingdong_ware_count = len(self.message["request"]["add_jingdong_order_bodys"]["add_jingdong_order_body"])
        self._robot_body = {
            "msgtype": "markdown",
            "markdown": {
                "title": title,
                "text": f"### {title}\n\n"
                        f"- #### 京东订单号：<font color=\"#4169E1\">{jingdong_order_id}</font>\n\n"
                        f"- #### 下单时间：<font color=\"#4169E1\">{order_create_date}</font>\n\n"
                        f"- #### 商品数量：<font color=\"#4169E1\">{jingdong_ware_count}</font>\n\n"
                        f"- #### 是否同步快普：<font color=\"#CD0000\">否</font>\n\n"
                        f"- #### 原因：<font color=\"#FF7256\">{jingdong_order_id}中有未映射到的SKU</font>\n\n"
                        f"- #### 机构：<font color=\"#9400D3\">{branch_id}</font>\n\n"
                        f"- 机器人通知时间：<font color=\"#4F4F4F\">{message_time}</font>\n\n"
                        f"@xxxxxxxxxxxxx",
            },
            "at": {
                "isAtAll": "false",
                "atMobiles": ["xxxxxxxxxxxxxxxxxxx"]
            },
        }
        # logs.info(self._robot_body)
        res = requests.post(url=self._robot_url, json=self._robot_body)
        logs.info(f"钉钉通知状态：{res.json()}")

    def admin_error_text_push(self):
        code = self.message["code"]
        result = self.message["result"]
        error = self.message["error"]
        retry = self.message["retry"]
        update = self.message["update"]
        message_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        if code == 2:
            error_robot_body_at = {
                "msgtype": "markdown",
                "markdown": {
                    "title": "京东商城订单通知——程序运行异常",
                    "text": f"### 京东商城订单通知——程序运行异常\n\n"
                            f"- #### 程序代码：<font color=\"#0000CD\">{code}</font>\n\n"
                            f"- #### 程序运行状态：<font color=\"#E4080A\">{result}</font>\n\n"
                            f"- #### 程序报错信息：<font color=\"#0000CD\">{error}</font>\n\n"
                            f"- #### 程序已重试：<font color=\"#0000CD\">{retry}</font>\n\n"
                            f"- #### 程序错误处理：<font color=\"#0000CD\">{update}</font>\n\n"
                            f"- #### 机构：<font color=\"#9400D3\">金穗隆(广州)信息科技有限公司</font>\n\n"
                            f"- 机器人通知时间：<font color=\"#4F4F4F\">{message_time}</font>\n\n"
                            f"@xxxxxxxxxxxxxxxxxxxxx",
                },
                "at": {
                    "isAtAll": "false",
                    "atMobiles": ["xxxxxxxxxxxxxxxxxxxx"]
                }
            }
            res = requests.post(url=self._robot_url, json=error_robot_body_at)

    def admin_info_text_push(self):
        code = self.message["code"]
        status = self.message["status"]
        result = self.message["result"]
        update = self.message["update"]
        message_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        if code == 3:
            error_robot_body_at = {
                "msgtype": "markdown",
                "markdown": {
                    "title": "京东商城订单通知——程序运行情况通知",
                    "text": f"### 京东商城订单通知——程序运行情况通知\n\n"
                            f"- #### 程序代码：<font color=\"#E4080A\">{code}</font>\n\n"
                            f"- #### 程序运行状态：<font color=\"#0000CD\">{result}</font>\n\n"
                            f"- #### 处理结果：<font color=\"#0000CD\">{update}</font>\n\n"
                            f"- #### 机构：<font color=\"#9400D3\">金穗隆(广州)信息科技有限公司</font>\n\n"
                            f"- 机器人通知时间：<font color=\"#4F4F4F\">{message_time}</font>\n\n"
                            f"@xxxxxxxxxxxxxxxxxxx",
                },
                "at": {
                    "isAtAll": "false",
                    "atMobiles": ["xxxxxxxxxxxxxxxxxxx"]
                }
            }
            res = requests.post(url=self._robot_url, json=error_robot_body_at)

    def image_push(self):
        title = "京东商城订单销售情况"
        print(self.message)
        message_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        photo_at = {
            "msgtype": "markdown",
            "markdown": {
                "title": title,
                "text": f"{title}"
                        f"![photo]({self.message})"
                        f"- 机器人通知时间：<font color=\"#4F4F4F\">{message_time}</font>\n\n"
                        f"@xxxxxxxxxxxxxxx"
            },

            "at": {
                "isAtAll": "false",
                "atMobiles": ["xxxxxxxxxxxxxxxxxxxxx"]
            }
        }
        res = requests.post(
            url=self._company_robot_url,
            json=photo_at)
        print(res.json())

    def delete_text_push(self):
        message_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        title = "京东商城订单通知"
        branch_id = self.message["request"]["add_jingdong_order"]['branch_id']
        jingdong_order_id = self.message["request"]["add_jingdong_order"]['jingdong_order_id']
        order_create_date = self.message["request"]["add_jingdong_order"]['order_create_date']
        jingdong_ware_count = len(self.message["request"]["add_jingdong_order_bodys"]["add_jingdong_order_body"])
        self._robot_body = {
            "msgtype": "markdown",
            "markdown": {
                "title": title,
                "text": f"### {title}\n\n"
                        f"- #### 京东订单号：<font color=\"#4169E1\">{jingdong_order_id}</font>\n\n"
                        f"- #### 下单时间：<font color=\"#4169E1\">{order_create_date}</font>\n\n"
                        f"- #### 商品数量：<font color=\"#4169E1\">{jingdong_ware_count}</font>\n\n"
                        f"- #### 京东订单状态：<font color=\"#CD0000\">订单已被删除</font>\n\n"
                        f"- #### 机构：<font color=\"#9400D3\">{branch_id}</font>\n\n"
                        f"- 机器人通知时间：<font color=\"#4F4F4F\">{message_time}</font>\n\n"
                        f"@xxxxxxxxxxxxxxx",
            },
            "at": {
                "isAtAll": "false",
                "atMobiles": ["xxxxxxxxxxxxxxxxxxxxxxxxxx"]
            },
        }
        # logs.info(self._robot_body)
        res = requests.post(url=self._robot_url, json=self._robot_body)
        logs.info(f"钉钉通知状态：{res.json()}")




